# Convert Unsloth model to vLLM compatible format
import torch
def convert_unsloth_to_vllm(unsloth_model_path, output_path):
    """
    将Unsloth微调的模型转换为vLLM兼容格式
    """
    from unsloth import FastLanguageModel
    from transformers import AutoModelForCausalLM, AutoTokenizer
    import shutil
    
    # 加载模型
    model, tokenizer = FastLanguageModel.from_pretrained(
        unsloth_model_path,
        dtype=torch.float16,  # 确保转换时使用合适的dtype
        load_in_4bit=True,
    )
    
    # 保存为标准格式
    model.save_pretrained(output_path)
    tokenizer.save_pretrained(output_path)
    
    print(f"模型已保存到: {output_path}")
    
    return model, tokenizer

# 使用示例
# model, tokenizer = convert_unsloth_to_vllm(
#     "/mnt/codes/qwen3/models/Qwen3-4B-unsloth-bnb-4bit",
#     "/mnt/codes/qwen3/models/Qwen3-4B-unsloth-bnb-4bit-compatible-vllm"
# )


import json
from typing import List, Dict, Any, Union

class JSONConverter:
    @staticmethod
    def json_to_jsonl(input_file: str, output_file: str, 
                     handle_nested: bool = True) -> None:
        """
        JSON 转 JSONL
        
        Args:
            input_file: 输入 JSON 文件路径
            output_file: 输出 JSONL 文件路径
            handle_nested: 是否处理嵌套对象
        """
        with open(input_file, 'r', encoding='utf-8') as f:
            data = json.load(f)
        
        with open(output_file, 'w', encoding='utf-8') as f:
            if isinstance(data, list):
                for item in data:
                    f.write(json.dumps(item, ensure_ascii=False) + '\n')
            else:
                f.write(json.dumps(data, ensure_ascii=False) + '\n')
                
        print(f"转换完成: {input_file} -> {output_file}")
    
    @staticmethod
    def jsonl_to_json(input_file: str, output_file: str) -> None:
        """
        JSONL 转 JSON
        """
        items = []
        with open(input_file, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                if line:
                    items.append(json.loads(line))
        
        with open(output_file, 'w', encoding='utf-8') as f:
            json.dump(items, f, ensure_ascii=False, indent=2)
            
        print(f"反向转换完成: {input_file} -> {output_file}")

# 使用示例
converter = JSONConverter()

# JSON 转 JSONL
converter.json_to_jsonl('/mnt/cache/modelscope/datasets/KuugoRen/chinese_law_ft_dataset/chinese_law_ft_dataset.json', '/mnt/cache/modelscope/datasets/KuugoRen/chinese_law_ft_dataset/chinese_law_ft_dataset.jsonl')

# JSONL 转 JSON  
# converter.jsonl_to_json('output.jsonl', 'recovered.json')
